//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    item: 0,
    tab: 0,
    state:'paused',
    playIndex:0,
    play:{
      currentTime:'00:00',
      duration:'00:00',
      percent:0,
      title:'123',
      singer:'123',
      coverImgUrl:'/images/cover.jpg',
    },
    playlist: [
      {
        id:1,
        title:'起风了',
        singer:"吴青峰",
        src:'https://link.hhtjim.com/163/1338695683.mp3',
        coverImgUrl:'http://p1.music.126.net/aMVPsO00OqlVTS2yMH8RgA==/109951163785600029.jpg?param=130y130'
      },
      {
        id:2,
        title:'I WANNA SEE YOU ANYMORE',
        singer:"NINE ONE",
        src:'https://link.hhtjim.com/163/528326686.mp3',
        coverImgUrl:'https://p2.music.126.net/a_gJIqhD4ICAC30UttQIGg==/109951163100083686.jpg?param=130y130'
      },
      {
        id:3,
        title:'不用去猜',
        singer:"Jony J",
        src:'https://link.hhtjim.com/163/490595927.mp3',
        coverImgUrl:'https://p2.music.126.net/h_1u3v9vTCRWN_ZJlVdQkQ==/18523472395121303.jpg?param=130y130'
      }
    ]
  },
  //事件处理函数
  changeItem: function(e) {
    this.setData({
      item: e.target.dataset.item
    })
  },
  changePage:function(e){
    this.setData({
      item: 2
    })
  },
  changeTab: function(e) {
    this.setData ({ 
      tab: e.detail.current
    })
  },
  play: function() {
    this.audioCtx.play()
    this.setData({ state: 'running' })
  },
  pause: function() {
    this.audioCtx.pause()
    this.setData({ state: 'paused' })
  },
  next: function() {
    var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1 
    this.setMusic(index)
    if ( this.data.state === 'running' ) {
      this.play()
    }
  },
  audioCtx:null,
  onReady: function() {
    this.audioCtx = wx.createInnerAudioContext()
    // 默认选择第1曲
    this.setMusic(0)
    var that = this 
    // 播放检测失败
    this.audioCtx.onError(function() {
      console.log('播放失败' + that.audioCtx.src)
    })
    // 播放完成自动换下一曲
    this.audioCtx.onEnded(function() {
      that.next()
    })
    // 自动更新播放进度
    this.audioCtx.onPlay(function(){})
    this.audioCtx.onTimeUpdate(function(){
      that.setData({
        'play.duration': formatTime(that.audioCtx.duration),
        'play.currentTime':formatTime(that.audioCtx.currentTime),
        'play.percent':that.audioCtx.currentTime / that.audioCtx.duration * 100
      })
    })
    // 格式化时间
    function formatTime(time){
      var minute = Math.floor(time / 60) % 60;
      var second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ":" + (second < 10 ? '0' + second : second)
    }
  },
  setMusic:function(index){
    var music = this.data.playlist[index]
    this.audioCtx.src = music.src
    this.setData({
      playIndex: index,
      'play.title':music.title,
      'play.singer':music.singer,
      'play.coverImgUrl':music.coverImgUrl,
      'play.currentTime':'00:00',
      'play.duration':'00:00',
      'play.percent': 0
    })
  },
  sliderChange: function(e) {
    var second = e.detail.value * this.audioCtx.duration / 100
    this.audioCtx.seek(second)
  },
  change: function(e) {
    this.setMusic(e.currentTarget.dataset.index)
    this.play()
  }
})
